package main

import (
	"net/http"
	"fmt"
	"time"
)

func SayHello(w http.ResponseWriter, req *http.Request) {
	fmt.Println(req.Header)
	w.Write([]byte("Hello"))
	if req.Close {
		
	}
}

func SayWold(w http.ResponseWriter, req *http.Request) {
	fmt.Println(req.Header)
	w.Write([]byte("Wold"))
}

// CloseNotifier经常被ResponseWriter用来检测底层连接是否已经断开，举例如下：
func Test(w http.ResponseWriter, r *http.Request) {
	//http.ServeFile(w, r, "test.go")
	var closenotify http.CloseNotifier
	closenotify = w.(http.CloseNotifier)
	select {
	case <-closenotify.CloseNotify():
		fmt.Println("cut")
	case <-time.After(time.Duration(100) * time.Second):
		fmt.Println("timeout")

	}
}

func main() {
	http.HandleFunc("/world", SayWold)
	http.HandleFunc("/hello", SayHello)
	http.HandleFunc("/test", Test)
	http.ListenAndServe(":8001", nil)

	////下面这种可以设置读取超时时间
	//server := &http.Server{
	//	Addr:";8001",
	//	Handler:nil,
	//	ReadTimeout:10*time.Second, //读取超时
	//}
	//server.ListenAndServe()
}
